package site.marqstree.kotlin.library.base.ui.widget.recyclerview

import androidx.annotation.LayoutRes
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.library.base.ui.widget.recyclerview
 * 文件名: DataBindingAdapter
 * 创建者: marqstree
 * 创建时间: 2020/3/19 18:25
 * 描述: TODO
 */
open class DataBindingAdapter<T:ViewDataBinding,B:Any>(
    //Item布局文件Id
    @LayoutRes val layoutId: Int,
    //数据绑定对象id
    val mBRDataId: Int,
    //presenter对象id
    val mBRPresenterId: Int = 0,
    //presenter对象
    val mPresenter: Any? = null)
: BaseQuickAdapter<B,BaseViewHolder>(layoutId){

    override fun onItemViewHolderCreated(viewHolder: BaseViewHolder, viewType: Int) {
        // 绑定 view
        DataBindingUtil.bind<ViewDataBinding>(viewHolder.itemView)
    }

    override fun convert(helper: BaseViewHolder, item: B) {
        // 获取 Binding
        val binding: T = helper.getBinding()!!
        if (binding != null) {
            //给布局文件bean绑定数据
            binding.setVariable(mBRDataId,item)
            //绑定Presenter对象
            mPresenter?.let{
                binding.setVariable(mBRPresenterId, mPresenter)
            }
            binding.executePendingBindings()
        }
    }
}